<?php
	class Message extends Controller{
		function Message(){
			parent::Controller();
			$this->load->model('messageModel');
		}
		function index(){
			$id=intval($id = $this->uri->segment(2));
			if(!$id){
				header('location:'.site_url());
				return;
			}
			$offset = floor(floatval($this->uri->segment(3)));
			if($offset<0)$offset=0;
			$data['messages']=$this->messageModel->read($id,$offset);
			$data['parent_id']=$id;
			
			$pagesize=20;
			$config['base_url'] = base_url().'/message/'.$id;
			$config['total_rows'] = $data['messages']['recordcount'];
			$config['per_page'] = $pagesize;
			$this->load->library('pagination'); 
			$this->pagination->initialize($config); 
			$data['pager']=$this->pagination->create_links();
			
			$this->load->view('message.php',$data);
		}
		function insert(){
			$user_id=$this->session->userdata['user_id'];
			if(!$user_id){
				header('location:'.site_url('member/login'));
				return;
			}
			if($_SERVER['REQUEST_METHOD']=='GET'){
				$data['id']=intval($id = $this->uri->segment(3));
				if(!$data['id']){
					header('location:'.site_url());
					return;
				}
				$this->load->view('insert',$data);
				return;
			}
			else{
				$data['parent_id']=floor(floatval($this->input->post('parent_id')));
				$data['title']=strip_tags($this->input->post('title'));
				$data['content']=strip_tags($this->input->post('content'));
				$data['forum_id']=intval($this->input->post('forum_id'));
				$data['user_id']=$user_id;
				$data['create_time']=date('Y-m-d H:i:s');
				if(!$data['parent_id'] && !$data['forum_id']){
					return;
				}
				$result=$this->messageModel->insert($data);
				if($result){
	 				$data['data']=array(
						'flag'=>1,
						'message'=>'发表成功',
						'link'=>'返回',
						'href'=>site_url('bbs/'.$data['parent_id']?('message/'.$data['parent_id']):('forum/'.$data['forum_id']))
					);
	 			}
	 			else{
	 				$data['data']=array(
						'flag'=>0,
						'message'=>'发表失败',
						'link'=>'返回',
						'href'=>site_url('bbs/'.$data['parent_id']?('message/'.$data['parent_id']):('forum/'.$data['forum_id']))
					);
	 			}
	 			$this->load->view('show_info',$data);
			}	
				
			
		}
	}
?>